package Ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import Messaging.Notification;
import Messaging.NotificationManager;
import Model.Dice;

/*
 * @author Paolo Cordova
 */

public class DicePanel extends JPanel 
{
	private JButton rollDice_;
	private JLabel die1_, die2_;
	private ImageIcon diePic_[];
	private Dice Dice_;
	private boolean diceState_ = true;
	int buttonCounter = 0;	
	private int savedState = 0;
	static final long serialVersionUID = 10;

	public DicePanel(Dice dice)
	{

		int rightMargin = 280, topMargin = 540;

		int buttonHeight = 30, dieSize = 80;
		int panelWidth = 160, panelHeight = buttonHeight + dieSize;

		this.setSize(panelWidth, panelHeight);
		this.setLayout(null);

		Dice_ = dice;

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

		Point location = new Point((int) (screen.getWidth() - rightMargin),
				topMargin);

		Font buttonFont = new Font("SansSerif", Font.BOLD, 18);

		rollDice_ = new JButton("Roll Dice");
		rollDice_.setBounds(0, 0, panelWidth, buttonHeight);
		rollDice_.setFont(buttonFont);
		rollDice_.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent event) 
			{
				rollDice_.setEnabled(false);
				NotificationManager.getInstance().notifyObservers(Notification.DISABLE_PURCHASE, null);
				Dice_.roll();
			}
		});

		diePic_ = new ImageIcon[6];

		for (int i = 0; i < diePic_.length; i++) 
		{
			diePic_[i] = new ImageIcon("MonopolyImages/Dice/die" + (i + 1) + ".jpg");
		}

		die1_ = new JLabel();
		die1_.setIcon(diePic_[0]);
		die1_.setBounds(0, buttonHeight, (panelWidth / 2), dieSize);

		die2_ = new JLabel();
		die2_.setIcon(diePic_[0]);
		die2_.setBounds((panelWidth - dieSize), buttonHeight, (panelWidth / 2),
				dieSize);

		this.add(rollDice_);
		this.add(die1_);
		this.add(die2_);

		DisplayAssembler.getInstance().addComponent(this, location,
				JLayeredPane.PALETTE_LAYER);

		this.setVisible(true);

		NotificationManager.getInstance().addObserver(Notification.ROLL_DICE,
				this, "updateDice");
		NotificationManager.getInstance().addObserver(Notification.END_TURN,
				this, "endTurn");
		NotificationManager.getInstance().addObserver(Notification.SHOW_CARD, 
				this, "setDisabled");
		NotificationManager.getInstance().addObserver(Notification.REMOVE_CARD, 
				this, "setEnabled");
		NotificationManager.getInstance().addObserver(Notification.END_TURN_EARLY,
				this, "forceEndTurn");
	}

	public JButton getRollButton()
	{
		return rollDice_;
	}

	public void updateDice(Object obj)
{

		try 
		{
			final Dice dice = (Dice) obj;

			new Thread() 
			{
				public void run() 
				{
					Random r = new Random();
					
					for (int i = 0; i < 20; i++) 
					{
						die1_.setIcon(diePic_[r.nextInt(6)]);
						die2_.setIcon(diePic_[r.nextInt(6)]);
					
						try 
						{
							Thread.sleep(25+(i*3));
						} 
						
						catch (InterruptedException e) 
						{
							e.printStackTrace();
						}
					}

					die1_.setIcon(diePic_[dice.getDie1() - 1]);
					die2_.setIcon(diePic_[dice.getDie2() - 1]);
					
					

					if (dice.getNumInRowDoubles() == 0) 
					{
						NotificationManager.getInstance().notifyObservers(Notification.DONE_ROLLING, null);		
						NotificationManager.getInstance().notifyObservers(Notification.DICE_ANIMATION_DONE, dice);
					}

					else if (dice.getNumInRowDoubles() < 3)
					{
						rollDice_.setForeground(Color.red);
						rollDice_.setText("Roll Again");
						rollDice_.setEnabled(true);
						NotificationManager.getInstance().notifyObservers(Notification.DICE_ANIMATION_DONE, dice);
					}
					
					else {
						rollDice_.setText("Go To Jail");
						NotificationManager.getInstance().notifyObservers(Notification.GO_TO_JAIL, null);
						NotificationManager.getInstance().notifyObservers(Notification.DONE_ROLLING, null);
					}
				}
			}.start();
		} 
		catch (ClassCastException e) {
			System.err.println("Dice expected object to method updateDice");
		}
	}

	public void endTurn() 
	{
		rollDice_.setForeground(Color.black);
		rollDice_.setText("Roll Dice");
		rollDice_.setEnabled(true);
	}
	
	public void forceEndTurn() 
	{
		rollDice_.setForeground(Color.red);
		rollDice_.setText("Go To Jail");
		rollDice_.setEnabled(false);
	}
	
	public void setDisabled() {
		if (savedState == 0) {
			diceState_ = rollDice_.isEnabled();
			rollDice_.setEnabled(false);
		}
		savedState++;
	}
	
	public void setEnabled() {
		if (savedState == 1)
			rollDice_.setEnabled(diceState_);
		savedState--;
	}
}
